package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Department;
import cn.wolfcode.mapper.DepartmentMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentServiceImpl implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public void save(Department department) {
        departmentMapper.insert(department);
    }

    @Override
    public void delete(Long id) {
        departmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Department department) {
        departmentMapper.updateByPrimaryKey(department);
    }

    @Override
    public Department get(Long id) {
        return departmentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Department> listALL() {
        return departmentMapper.selectAll();
    }

    @Override
    public PageInfo<Department> query(QueryObject qo) {
        // 查询数量
       /* int count = departmentMapper.selectForCount(qo);
        if(count == 0) {
            pageInfo<Department> pageInfo = new pageInfo<>(qo.getCurrentPage(),
                    qo.getPageSize(), count, Collections.emptyList());
            return pageInfo;
        }

        //获取到总页数
        int totalPage = count % qo.getPageSize() == 0
                ? count / qo.getPageSize() : count / qo.getPageSize() + 1;
        //判断当前页是否大于总页数，如果是，把总页数作为当前页来查询分页结果集
        if(qo.getCurrentPage()>totalPage){
            qo.setCurrentPage(totalPage);
        }
*/
        //告诉插件要开始分页了，传入当前页，每页显示数据
        //调用了 PageHelper.startPage的方法后，会下一句准备要执行的sql进行分页
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Department> departments = departmentMapper.selectForList(qo);
        return new PageInfo<>(departments);
    }

}
